/**
 * 文件名称:  DictServiceImpl.java
 * 文件序号:  序号
 * 内容描述:  文件简单描述，多行换行书写
 * 版权声明:  山东鸿蝶信息技术有限公司
 * 创建用户:  zhangkai
 * 创建日期:  2017/1/16 08:24
 * 修改用户:  无
 * 修改日期:  无
 * 修改说明:  无
 * 文件版本:  V1.0
 * 文件路径:
 */

package com.hongdee.base.brand.service.impl;


import com.hongdee.base.Constant;
import com.hongdee.base.brand.dao.IBrandDao;
import com.hongdee.base.brand.entity.TBcBrand;
import com.hongdee.base.brand.service.IBrandService;
import com.hongdee.common.util.ImageUtils;
import com.hongdee.common.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 名  称:BrandServiceImpl
 * 类  型: class
 * 描  述: 用一句话描述类的作用
 * 路  径:com.hongdee.base.brand.service.impl
 * 作  者:zhangkai
 * 日  期:2017/1/16 08:24
 */
@Service
public class BrandServiceImpl implements IBrandService {
    @Autowired
    private IBrandDao brandDao;

    private String tableName = "T_BC_BRAND";

    /**
     * @param start       起始记录
     * @param limit       页面大小
     * @param brandCode   查询 SQL
     * @param brandName   参数数组
     * @param produceName 生产厂家
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 张国凯
     */
    @Override
    public Map page(String start, String limit, String brandCode, String brandName, String produceName) throws Exception {
        String sqlStr = "select * from " + tableName + " where 1 = 1 ";
        List<Object> params = new ArrayList();
        if (!StringUtils.isEmpty(brandCode)) {
            sqlStr += " and b_code like ? ";
            params.add("%" + brandCode + "%");
        }
        if (!StringUtils.isEmpty(brandName)) {
            sqlStr += " and  b_name like ? ";
            params.add("%" + brandName + "%");
        }
        if (!StringUtils.isEmpty(produceName)) {
            sqlStr += " and produce_code = ? ";
            params.add(produceName);
        }
        return this.brandDao.page(sqlStr, params.toArray(), start, limit);
    }

    /**
     * @param id 主键 ID
     * @return Map
     * @throws Exception
     * @desc 查询详情
     * @author 张国凯
     */
    @Override
    public Map info(String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择品牌");
        }
        String sqlStr = "select * from " + tableName + " where id = ?";
        return this.brandDao.info(sqlStr, new Object[]{id});
    }

    /**
     * @param id     主键 ID
     * @param status 状态
     * @return
     * @throws Exception
     * @desc 修改状态
     * @author 张国凯
     */
    @Override
    public void update(String id, String status) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择品牌");
        }
        String hql = " update TBcBrand set status =:status where id = :id";
        Map params = new HashMap();
        params.put("id", id);
        params.put("status", status);
        this.brandDao.update(hql, params);
    }
    @Override
    public void newupdate(List<Map> data, String status) throws Exception {
        if (data.isEmpty()) {
            throw new Exception("请选择品牌");
        }
        String sql = " update T_Bc_Brand set status = ? where id in ( ";
        List params = new ArrayList();
        params.add(status);
        for(Map idMap :data){
            sql+=" ? , ";
            params.add(idMap.get("id"));
        }
       sql+=" '' ) ";
        this.brandDao.executeSQL(sql, params.toArray());
    }

    /**
     * @param data 要保存的变更历史记录实体
     * @return TBcBrand
     * @throws Exception
     * @desc 保存添加或者修改的实体内容，通过主键判断
     * @author 张国凯
     */
    @Override
    public TBcBrand saveOrUpdate(TBcBrand data) throws Exception {
        if (null == data.getId() || StringUtils.isEmpty(data.getId())) {
            //新增时判断是否已经存在
            if (existBrand(data.getbCode(), data.getId(), Constant.ADD)) {
                throw new Exception("该品牌已经存在");
            }
        } else {
            //保存时，是否存在
            if (existBrand(data.getbCode(), data.getId(), Constant.UPDATE)) {
                throw new Exception("该品牌已经存在");
            }
        }

        this.brandDao.saveOrUpdate(data);
        return data;
    }

    /**
     * @param bCode 品牌编码
     * @param id    品牌
     * @param type  操作类型
     * @return bool
     * @throws Exception 基类异常
     * @desc 判断品牌编码是否重复
     * @author zhangkai
     * @date 2017/2/9
     * @time 16:30
     * @update_user
     * @update_time
     * @update_desc:
     */
    private boolean existBrand(String bCode, String id, int type) throws Exception {
        String sql = "select * from " + tableName + " where b_code = ? ";
        List param = new ArrayList();
        param.add(bCode);
        if (type == Constant.UPDATE) {
            sql += " and id != ? ";
            param.add(id);
        }
        List resList = this.brandDao.list(sql, param.toArray());
        return resList != null && resList.size() > 0;
    }

    /**
     * @return List
     * @throws Exception
     * @desc 品牌下拉列表
     * @author 张国凯
     */
    @Override
    public List<Map> combo() throws Exception {

        String sql = " select * from " + tableName + " where status ='1'  ";
        List<Map<String, Object>> mapList = this.brandDao.list(sql);
        List<Map> resList = new ArrayList<Map>();
        Map temp;
        for (Map map : mapList) {
            temp = new HashMap();
            temp.put("value", map.get("bCode"));
            temp.put("label", map.get("bName"));
            resList.add(temp);

        }
        return resList;
    }


    /**
     * @return Map
     * @throws Exception
     * @desc 品牌编码转换
     * @author 张国凯
     */
    @Override
    public Map convert() throws Exception {
        String sql = " select * from " + tableName + "  ";
        List<Map<String, Object>> mapList = this.brandDao.list(sql);
        List<Map> resList = new ArrayList<Map>();
        Map temp = new HashMap();
        for (Map map : mapList) {
            temp.put(map.get("bCode"), map.get("bName"));
        }
        return temp;
    }
    /**
     * @Name: 图片写入数据库
     * @Author: dengbin
     * @Date: 2017/11/22 09:57
     * @Remark:
     */
    @Override
    public void writePic(MultipartFile image, String id) throws Exception {

        FileInputStream fileInputStream = ImageUtils.getFileInputStream(image);
        List params = new ArrayList();
        String sql = "update " + tableName +"  set pic = ? where id = ? ";
        params.add(fileInputStream);
        params.add(id);
        brandDao.executeSQL(sql,params.toArray());

    }

    /**
     * @Name: 从数据库取出图片
     * @Author: dengbin
     * @Date: 2017/11/22 09:57
     * @Remark:
     */
    @Override
    public byte[] readPic(String uid, String id) throws Exception {
        String sql = "select * from "+  tableName +" where id = ? "; //o_disp_order <> '0'
        List params = new ArrayList();
        params.add(id);
        Map map =  brandDao.info(sql,params.toArray());
        List list = brandDao.list(sql,params.toArray());
        System.out.println(list);
        Object pic =  map.get("pic");
        byte[] bytes = (byte[]) pic;
        return bytes;
    }
}
